package com.bdqn.controller;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.dto.Dto;
import com.bdqn.dto.MsgDto;
import com.bdqn.model.ContactList;
import com.bdqn.model.CustomerBaseInfo;
import com.bdqn.service.ContactListService;
import com.bdqn.service.CustomService;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("/custom")
public class CustomController {

	@Autowired
	private CustomService customService;
	
	@Autowired
	private ContactListService contactListService;
	
	//客户模糊查询
	@RequestMapping("/custList")
	@ResponseBody
	public List<CustomerBaseInfo> custList(@RequestParam("companyName") String companyName,@RequestParam("agent") String agent) {
		
		List<CustomerBaseInfo> custs = new ArrayList<>();
		
		
		
		if(companyName != null&&companyName !="") {
			
			custs = customService.selectLikeName(companyName,agent);
		
		}else {
			
			custs = customService.selectAll(agent);
		}
	
		return custs;					
	}
	
	//客户模糊查询
		@RequestMapping("/likeSearch")
		public String likeSearch(@RequestParam("companyName") String companyName,@RequestParam("agent") String agent,Model model) {
			
			PageInfo<CustomerBaseInfo> cs = new PageInfo<>();
			
			boolean isExist = true;
			
			if(companyName==null||companyName=="") {
				cs= customService.Search(agent);
			}else {
				cs=customService.likeSearch(companyName,agent);
				
				isExist=customService.searchExist(companyName,agent);
			}
			
			
			model.addAttribute("cs", cs);
			
		//	model.addAttribute("message", message);
			
			model.addAttribute("companyName", companyName);
			
			if(!isExist) {
				model.addAttribute("msg", "客户名称不存在请重新搜索！");
			}
			
			return "customManage";					
		}
		
		//分页查询
		@RequestMapping("/page")
		public String page(Model model,Dto dto,@RequestParam("agent") String agent){
				
			PageInfo<CustomerBaseInfo> cs = customService.page(dto, agent);
			
			model.addAttribute("cs", cs);
			
			model.addAttribute("companyName", dto.getWord());

			return "customManage";			
		}
		
		@RequestMapping("/check/{id}")
		public String check(@PathVariable("id")Integer id,Model model) {
			
			CustomerBaseInfo cust = customService.check(id);
			
			List<ContactList> cl = contactListService.contactList(cust);
			
			model.addAttribute("cust", cust);
			
			model.addAttribute("cl", cl);
		
			return "custInfo";				
		}
		@RequestMapping("/toAlter/{id}")
		public String toAlter(@PathVariable("id")Integer id,Model model) {
			
			CustomerBaseInfo cust = customService.check(id);
			
			List<ContactList> cl = contactListService.contactList(cust);
			
			model.addAttribute("cust", cust);
			
			model.addAttribute("cl", cl);
		
			return "alterCustInfo";				
		}
		
		@RequestMapping("/Alter")
		@ResponseBody
		public MsgDto Alter(CustomerBaseInfo cust,Model model) {
			
			MsgDto msg = new MsgDto();
			
			int num = customService.alter(cust);
			
			if(num ==1) {
				msg.setCode(1);
				msg.setMessage("保存成功");
			}
			
		
			return msg;				
		}
		@RequestMapping("/state/{id}")
		public String state(@PathVariable("id")Integer id,@RequestParam("status")String state,
				@RequestParam("companyName")String companyName,Model model) {
			
			if(state.equals("启用")) {
				
				state="停用";
				
			}else {
				state="启用";
				
			}
			int num = customService.changeState(id,state);
			
			if(num==1) {
				
				CustomerBaseInfo cl = customService.check(id);
				
				String agent=cl.getAgent();
				
				PageInfo<CustomerBaseInfo> cs = new PageInfo<>();
				
				if(companyName==null||companyName=="") {
					cs= customService.Search(agent);
				}else {
					cs=customService.likeSearch(companyName,agent);
				}
				
				model.addAttribute("cs", cs);
				
				model.addAttribute("companyName", companyName);
				
				return "customManage";	
			}
			return null;
				
		
			
		}
}
